package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.google.api.client.util.Lists;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.ListUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.CarbonIndustryBo;
import com.ruoyi.system.domain.vo.CarbonIndustryVo;
import com.ruoyi.system.domain.CarbonIndustry;
import com.ruoyi.system.mapper.CarbonIndustryMapper;
import com.ruoyi.system.service.ICarbonIndustryService;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 行业Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-29
 */
@RequiredArgsConstructor
@Service
public class CarbonIndustryServiceImpl implements ICarbonIndustryService {

    private final CarbonIndustryMapper baseMapper;

    /**
     * 查询行业
     */
    @Override
    public CarbonIndustryVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询行业列表
     */
    @Override
    public TableDataInfo<CarbonIndustryVo> queryPageList(CarbonIndustryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CarbonIndustry> lqw = buildQueryWrapper(bo);
        Page<CarbonIndustryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询行业列表
     */
    @Override
    public List<CarbonIndustryVo> queryList(CarbonIndustryBo bo) {
        LambdaQueryWrapper<CarbonIndustry> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CarbonIndustry> buildQueryWrapper(CarbonIndustryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CarbonIndustry> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), CarbonIndustry::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), CarbonIndustry::getCode, bo.getCode());
        lqw.eq(StringUtils.isNotBlank(bo.getParentCode()), CarbonIndustry::getParentCode, bo.getParentCode());
        return lqw;
    }

    /**
     * 新增行业
     */
    @Override
    public Boolean insertByBo(CarbonIndustryBo bo) {
        CarbonIndustry add = BeanUtil.toBean(bo, CarbonIndustry.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改行业
     */
    @Override
    public Boolean updateByBo(CarbonIndustryBo bo) {
        CarbonIndustry update = BeanUtil.toBean(bo, CarbonIndustry.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CarbonIndustry entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除行业
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }


    @Override
    public void importData(List<CarbonIndustryBo> carbonIndustryBos) {
        if (CollectionUtils.isEmpty(carbonIndustryBos)){
            return;
        }
        //获取数据
        List<CarbonIndustry> carbonIndustries = importData(carbonIndustryBos, 1);
        importDataInsert(carbonIndustries);
    }

    @Override
    public List<CarbonIndustry> importData(List<CarbonIndustryBo> carbonIndustryBos, int level) {
        List<CarbonIndustry> cars = new ArrayList<>(100);
        for (CarbonIndustryBo carbonIndustryBo : carbonIndustryBos) {
            CarbonIndustry car = BeanUtil.toBean(carbonIndustryBo, CarbonIndustry.class);
            car.setLevel(level);//层级
            cars.add(car);
            List<CarbonIndustryBo> children = carbonIndustryBo.getChildren();
            if (!CollectionUtils.isEmpty(children)){
                cars.addAll(importData(children,level+1));
            }
        }
        return cars;
    }

    private void importDataInsert(List<CarbonIndustry> carbonIndustries){
        List<List<CarbonIndustry>> partition = ListUtils.partition(carbonIndustries, 2000);
        for (List<CarbonIndustry> industries : partition) {
            baseMapper.insertBatch(industries);
        }
    }
}
